<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="85px">
      <el-form-item label="商品" prop="name" label-width="45px">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入商品"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--      <el-form-item label="昵称" prop="nickname">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.nickname"-->
      <!--          placeholder="请输入昵称"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="商品描述" prop="describes">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.describes"-->
      <!--          placeholder="请输入商品描述"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->

      <el-form-item label="价格" prop="price" label-width="60px">
        <!--        <el-input-->
        <!--          v-model="queryParams.price"-->
        <!--          placeholder="请输入价格"-->
        <!--          clearable-->
        <!--          @keyup.enter.native="handleQuery"-->
        <!--        />-->

        <el-select v-model="queryParams.price" placeholder="请选择商品价格">
          <el-option label="0-50" value="50"></el-option>
          <el-option label="50-100" value="100"></el-option>
          <el-option label="100-150" value="150"></el-option>
          <el-option label="150-200" value="200"></el-option>
          <el-option label="200-250" value="250"></el-option>
          <el-option label="250-300" value="300"></el-option>
          <el-option label="300-350" value="350"></el-option>
          <el-option label="350-400" value="400"></el-option>
        </el-select>
      </el-form-item>


      <!--      <el-form-item label="单位" prop="unit">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.unit"-->
      <!--          placeholder="请输入单位"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item label="所属系列" prop="herbId">
        <!--        <el-input-->
        <!--          v-model="queryParams.herbId"-->
        <!--          placeholder="请输入所属系列"-->
        <!--          clearable-->
        <!--          @keyup.enter.native="handleQuery"-->
        <!--        />-->
        <el-select v-model="queryParams.herbId" placeholder="请选择本草系列">
          <el-option label="本草·同源(1)" value="1"></el-option>
          <el-option label="本草·沉香(2)" value="2"></el-option>
          <el-option label="本草·玉颜(3)" value="3"></el-option>
          <el-option label="本草·长生(4)" value="4"></el-option>
          <el-option label="本草·育灵(5)" value="5"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <span></span>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['goods:goods:add']"
        >新增
        </el-button>
      </el-col>
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="success"-->
      <!--          plain-->
      <!--          icon="el-icon-edit"-->
      <!--          size="mini"-->
      <!--          :disabled="single"-->
      <!--          @click="handleUpdate"-->
      <!--          v-hasPermi="['goods:goods:edit']"-->
      <!--        >修改</el-button>-->
      <!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['goods:goods:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['goods:goods:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="goodsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="编号" align="center" prop="id"/>
      <el-table-column label="商品" align="center" prop="name"/>
      <el-table-column label="昵称" align="center" prop="nickname"/>
      <el-table-column label="商品描述" align="center" prop="describes"/>
      <el-table-column label="价格" align="center" prop="price"/>
      <el-table-column label="单位" align="center" prop="unit"/>
      <el-table-column label="所属系列" align="center" prop="herbId"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">

            <el-button
              size="mini"
              type="text"
              icon="el-icon-search"
              @click="handleResearch(scope.row)"
            >查看
            </el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['goods:goods:edit']"
            style="color: #ffb70a;"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['goods:goods:remove']"
            style="color: #ff522e;"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商品对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="商品" prop="name">
          <el-input v-model="form.name" placeholder="请输入商品"/>
        </el-form-item>
        <el-form-item label="昵称" prop="nickname">
          <el-input v-model="form.nickname" placeholder="请输入昵称"/>
        </el-form-item>
        <el-form-item label="商品描述" prop="describes">
          <el-input v-model="form.describes" placeholder="请输入商品描述"/>
        </el-form-item>
        <el-form-item label="价格" prop="price">
          <el-input v-model="form.price" placeholder="请输入价格"/>
        </el-form-item>
        <el-form-item label="单位" prop="unit">
          <el-input v-model="form.unit" placeholder="请输入单位"/>
        </el-form-item>
        <el-form-item label="所属系列" prop="herbId">
          <el-input v-model="form.herbId" placeholder="请输入所属系列"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 弹出对话框 -->
    <el-dialog
      :title=form.name
      :visible.sync="dialogVisible"
      width="500px"
      @close="handleClose">
      <center><img :src="imageUrl" alt="抱歉，暂无商品图片！" style="width: 300px;height: 300px;" ></center>
      <span slot="footer" class="dialog-footer">
              <el-button @click="dialogVisible = false">关闭</el-button>
              </span>
    </el-dialog>
  </div>
</template>

<script>
import {listGoods, getGoods, delGoods, addGoods, updateGoods} from "@/api/goods/goods";
import {Row} from "element-ui";

export default {
  name: "Goods",
  computed: {
    Row() {
      return Row
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品表格数据
      goodsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        nickname: null,
        describes: null,
        price: null,
        unit: null,
        herbId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          {required: true, message: "名字不能为空", trigger: "blur"}
        ],
        price: [
          {required: true, message: "价格不能为空", trigger: "blur"}
        ],
      },

      dialogVisible: false,
      imageUrl: ''  // 这里放置要显示的图片URL
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询商品列表 */
    getList() {
      this.loading = true;
      listGoods(this.queryParams).then(response => {
        this.goodsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      console.log(this.queryParams.price)
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        nickname: null,
        describes: null,
        price: null,
        unit: null,
        herbId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商品";
    },


    /** Pay查看按钮操作 */
    handleResearch(row) {
      setTimeout(() => {
        // 根据需要更新图片的URL
        this.imageUrl = 'https://sky-paiad.oss-cn-beijing.aliyuncs.com/herbseeker/'+row.id+'.png';  // 替换为你的图片URL
        this.dialogVisible = true;
        this.form.name = row.name
        console.log('500ms后执行的方法');
      }, 500);

    },
    handleClose() {
      this.dialogVisible = false;
    },


    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getGoods(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改商品";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateGoods(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addGoods(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除商品编号为"' + ids + '"的数据项？').then(function () {
        return delGoods(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('goods/goods/export', {
        ...this.queryParams
      }, `goods_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
